<template>
  <v-chart class="chart" :option="option" />
</template>

<script>
import { use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { PieChart } from "echarts/charts";
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent,
} from "echarts/components";
import VChart, { THEME_KEY } from "vue-echarts";
import { defineComponent } from "vue";

use([
  CanvasRenderer,
  PieChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent,
]);

import { getAll } from "@/api/category";

export default defineComponent({
  name: "CategoryPie",
  components: {
    VChart,
  },
  data() {
    return {
      categories: [],
      categoriesDto: [],
      option: {
        title: {
          text: "分类",
          left: "center",
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          orient: "vertical",
          left: "left",
          data: this.categories,
        },
        series: [
          {
            name: "博客数量",
            type: "pie",
            radius: "55%",
            center: ["50%", "60%"],
            data: this.categoriesDto,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      },
    };
  },
  watch: {
    categories: {
      handler(newVal) {
        (this.option.legend = {
          orient: "vertical",
          left: "left",
          data: newVal,
        }),
          (this.option.series = [
            {
              name: "博客数量",
              type: "pie",
              radius: "55%",
              center: ["50%", "60%"],
              data: this.categoriesDto,
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: "rgba(0, 0, 0, 0.5)",
                },
              },
            },
          ]),
          console.log("new:" + newVal);
      },
    },
  },
  mounted() {
    this.getAllCategories();
  },
  provide: {
    [THEME_KEY]: "white",
  },
  // setup() {
  //   const option = ref({
  //     title: {
  //       text: "分类",
  //       left: "center",
  //     },
  //     tooltip: {
  //       trigger: "item",
  //       formatter: "{a} <br/>{b} : {c} ({d}%)",
  //     },
  //     legend: {
  //       orient: "vertical",
  //       left: "left",
  //       data: this.categories,
  //     },
  //     series: [
  //       {
  //         name: "Traffic Sources",
  //         type: "pie",
  //         radius: "55%",
  //         center: ["50%", "60%"],
  //         data: this.categoriesDto,
  //         emphasis: {
  //           itemStyle: {
  //             shadowBlur: 10,
  //             shadowOffsetX: 0,
  //             shadowColor: "rgba(0, 0, 0, 0.5)",
  //           },
  //         },
  //       },
  //     ],
  //   });

  //   return { option };
  // },
  methods: {
    getAllCategories() {
      const userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
      getAll(userInfo.id).then((res) => {
        if (res.code == 200) {
          let categories = [];
          let categoriesDto = [];
          let data = res.data;
          for (let i = 0; i < data.length; i++) {
            categories.push(data[i].title);
            let category = { value: data[i].blogTotal, name: data[i].title };
            categoriesDto.push(category);
          }
          this.categories = categories;
          this.categoriesDto = categoriesDto;
        }
      });
    },
  },
});
</script>
<style lang="less" scoped>
.chart {
  height: 80vh;
}
</style>
